<?php

class HTech_Controller_Action_Helper_Add extends Zend_Controller_Action_Helper_Abstract
{
    private $_model;
    private $_form;
    private $_redirect = 'view';
    private $_flashMessenger = false; 
    
    public function setModel(HTech_Db_Table $model)
    {
        $this->_model = $model;
        return $this;
    }
    
    public function setForm(HTech_Form $form)
    {
        $this->_form = $form;
        return $this;
    }
            
    public function setRedirect($redirect = null)
    {
        if($redirect) $this->_redirect = $redirect;
        return $this;
    }
                    
    public function setFlashMessenger($flash)
    {
        if(is_bool($flash)) $this->_flashMessenger = $flash;
        return $this;
    }
    
    public function process()
    {
        if ($this->getRequest()->isPost()) {
            /*
             * Form submited
             */
            if ($this->_form->isValid($this->getRequest()->getPost())) {
                /*
                 * Form valid
                 */
                try {
                    $id = $this->_model->save($this->_form->getValues());
                } catch (Exception $e) {
                    $this->_abort($e->getMessage()); 
                }
                /*
                 * Update completed
                 */
                if($this->_flashMessenger) {
                    $this->getActionController()->getHelper('flashMessenger')
                        ->addMessage(array('scs' => $this->_form->translate("Operation successfully completed!")));
                }
                if($this->_redirect) {
                    $action = is_array($this->_redirect) ? $this->_redirect[0] : $this->_redirect;
                    $controller = is_array($this->_redirect) ? $this->_redirect[1] : null;
                    $module = is_array($this->_redirect) ? $this->_redirect[2] : null;
                    if(is_array($this->_redirect) && isset($this->_redirect[3]) && is_array($this->_redirect[3])) {
                        $params = is_array($this->_redirect[3][0]) ? $this->_redirect[3][0] : 
                            array($this->_redirect[3][0] => $this->_form->getValue($this->_redirect[3][1]));
                    } else {
                        $params = array('id' => $id);
                    } //printf('/%s/%s/%s/%s/%s',$module,$controller,$action,key($params),$params[0]);exit;
                    $this->_actionController->getHelper('Redirector')
                        ->gotoSimple($action,$controller,$module,$params);
                    /*
                    $this->_actionController->getHelper('Redirector')
                        ->gotoSimple(
                            is_array($this->_redirect) ? $this->_redirect[0] : $this->_redirect , 
                            is_array($this->_redirect) ? $this->_redirect[1] : null,
                            is_array($this->_redirect) ? $this->_redirect[2] : null, 
                            (is_array($this->_redirect) && isset($this->_redirect[3]) && is_array($this->_redirect[3])) ? 
                                array($this->_redirect[3][0] => $this->_form->getValue($this->_redirect[3][1])) : array('id' => $id));
                     * 
                     */
                    exit();
                }
                return $id;
            } else { 
                $this->_form->setDescription($this->_form->translate("Operation can't be completed. Please check the form and try again."));
            }  
        } else { 
            /*
             * Form not submitted yet
             */
            $id = (int) $this->getRequest()->getParam('id');
            if($id) {
                try {
                    $this->_form->setDefaults($this->_model->getRecord($id)->d->toArray() + (array) $this->_model->getRecord($id)->l);
                } catch (Exception $e) {
                    $this->_abort($e->getMessage()); 
                }
            }
            $messages = $this->getActionController()->getHelper('flashMessenger')->getMessages();
            if (is_array(current($messages))) {
                $this->_form->setDescription(current(current($messages)));
            }           
        }
        return;
    }
    
    private function _abort($msg)
    {
        $this->getActionController()->getHelper('flashMessenger')->addMessage($msg);
        $this->getActionController()->redirect('/default/auth/deny');
    }
}